package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: Gavin
 * @Date: 9/11/2023 4:07 PM
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    CustomerDao customerDao;
    @Override
    public Map<String, Object> queryPageCustomers(Integer page, Integer rows, String customerName) {
        //1.计算分页的数据
        Integer jumpCount = (page - 1) * rows;

        //2.查询分页的数据
        List<Customer> customers = customerDao.queryPageCustomer(jumpCount,rows,customerName);

        //3.查询分页前的total的总记录数
        Integer total = customerDao.queryPageCustomerCount(customerName);

        //4.组装map数据并返回
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",customers);
        return map;
    }

    @Override
    public void save(Customer customer) {
        customerDao.save(customer);
    }

    @Override
    public Integer update(Integer customerId, Customer customer) {
        //其实不用设置customer的id,因为传递过来的customer里面已经含有了customer的id了
        customer.setCustomerId(customerId);
        return customerDao.update(customer);
    }

    @Override
    public void deleteCustomersByIds(String ids) {
        String[] split = StringUtils.split(ids, ",");
        List<String> idList = Arrays.asList(split);
        customerDao.deleteCustomersByIds(idList);
    }
}
